<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="css/reset.css" type="text/css" rel="stylesheet" >
<link href="css/common.css" type="text/css" rel="stylesheet" >
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/common.js"></script>
<meta name="keywords" content="" />
<meta name="description" content="" />
<style type="text/css">
/*定义购物车内商品的数量样式*/
	#shopnum{
		text-align: center;
		size: 2;
	}
	/*提交订单按钮样式*/
	#cenCar{
		display: inline-block;
		width: 70px;
		height: 30px;
		line-height: 30px;
		background-color: #E4393C;
		text-align: center;
		color:#fff;
		text-decoration: none;
	}
</style>
<title>购物车</title>

</head>
<body>
	<?php session_start()?>
	<!-- 头部开始 -->
	<div id="header">
		<div class='position'>
			<div class='logo'>
				<a style="line-height:60px; color:#CA141C;" href="../index.php">华为商城</a>
				<a style="font-size:16px;color: #CA141C" href="/">www.vmall.com</a>
			</div>
			<div class='search'>
				<div class='search-bar' style="border-color:#CA141C">
					<form action="../list.php" method="get">
					<input class='s-text' type="text" name="search" style="border-color:#CA141C">
					<input class='s-submit' type="submit" value='搜索' style="background-color: #CA141C">
				</form>	
				</div>
			</div>
		</div>
	</div>
	<!-- 头部结束 -->
	<!-- 导航开始-->
	<div id="nav" style="background-color: #3A3A3A">
		<div class='position' style="background-color: #3A3A3A">
			<!-- 分类相关 -->
			<?php
				include("../../public/config.php");
				$link = mysqli_connect(HOST,USER,PASS,DBNAME) or die("数据连接失败！");
				mysqli_set_charset($link,"utf8");
				$sql = "select * from type";

				$result = mysqli_query($link,$sql);

			?>
			<!-- 购物车导航栏 -->
			<div class='category'>
				<a class='active' href="../index.php" style="background-color: #3A3A3A">首页</a>
				<?php
					for ($i=1; $i < 4; $i++) { 
					$row = mysqli_fetch_assoc($result);
					echo '<a href="../list.php?tid='.$row['id'].'">'.$row['name'].'</a>';
					}
				?>
			</div>
			<!-- 用户相关 -->
			
			<div id="user-relevance" class='user-relevance'>
				<!--登录注册-->
				<?php
					// 判断用户是否登录
					if (!empty($_SESSION['adminuser'])) {
						echo '<div class="user-nav" style="color:#fff">';
						// 若登录则在购物车显示用户名称，若还没登录则显示登录注册按钮
						echo '<a href="../userAdmin/index.php" style="color:#fff;display:inline-block;margin-right:20px;">您好：'.(($_SESSION[adminuser][name]!="")?$_SESSION[adminuser][name]:$_SESSION[adminuser][username]).'</a>';
						echo '<a href="../../admin/useraction.php?a=logout&b=indexID" style="color:#fff;display:inline-block;">退出</a>';
						echo '</div>';
					}else{
						echo '<div class="user-nav login-reg">
								<a class="title" href="../reg/reg.php">注册</a>
							  </div>
							  <div class="user-nav login-reg">	
								<a class="title" href="../reg/login.php">登录</a>
							  </div>';
					}
				?>
			</div>
		</div>
	</div> 
	<!-- 导航结束 -->
	<link href="css/login/css/userhome.css" type="text/css" rel="stylesheet" >
    <script type="text/javascript" src="css/login/js/userhome.js"></script>	
	<!-- 主体部分 -->
    <div id="main">
	<?php
		switch ($_GET['a']) {
			case "add":
				$id = $_GET['id'];								//接收商品的id值
				$link = mysqli_connect(HOST,USER,PASS,DBNAME) or die("数据库连接失败！");//连接数据库

				mysqli_set_charset($link,"utf8");				//设置数据库的编码
				$sql = "select * from goods where id=".$id;		//查找商品数据
				$result = mysqli_query($link,$sql);				//执行sql语句

				if(empty($result)){								//判断数据库中有无该商品
					die("没有要添加的商品");
				}

				$shop = mysqli_fetch_assoc($result);			//解析结果集

				if(empty($_SESSION['shoplist'][$id])){			//判断购物车内有没有该商品
					$_SESSION['shoplist'][$id] = $shop;			//若无则添加该商品
					$_SESSION['shoplist'][$id]['num'] = 1;		
					echo '<p style="font-size:24px; margin:20px;color:red">成功放进购物车</p>';
				}else{
					$_SESSION['shoplist'][$id]['num'] +=1;		//如果购物车有该商品则数量加一
				}
				
				break;

			case "reduce":
				$id = $_GET['id'];
				if($_SESSION["shoplist"][$id]['num']<2){		//购物车的商品减一
					$_SESSION["shoplist"][$id]['num']=1;		//商品数量减为一时显示为1
				}else{
					$_SESSION["shoplist"][$id]['num']-=1;		//商品数量减一
				}
                break;

			case "del":
				unset($_SESSION['shoplist'][$_GET['id']]);		//删除商品
				echo '<p style="font-size:24px; margin:20px;color:red">删除成功</p>';
				break;

			case "clear":
				unset($_SESSION['shoplist']);					//清空购物车
				// echo '<p style="font-size:24px; margin:20px;color:red">删除成功</p>';

				break;
			
			}
		?>





	<div class='left' style="width: 100%;">
	<div id="content">
	<!-- 载入公共头部文件-->
	<link href="css/login/css/cart.css" type="text/css" rel="stylesheet" >
	<div id="main">
		<div class='step'>
			<div class='cart-title'>
				<i></i>
				<div>
					<h3>
						我的购物车
					</h3>
					<p>
						<span class='cart-bar-bg'>
							<span></span>
						</span>
					</p>
				</div>
			</div>	
		</div>
		<!-- 购物车列表 -->
		<form action="order.php" method="post">
		<table class='cart-table'>
			<thead border=1;>
				<tr>
					<th width='15%' align='center'>名称</th>
					<th width='20%' align='center'>图片</th>
					<th width='10%' align='center'>单价</th>
					<th width='10%' align='center'>数量</th>
					<th width='10%' align='center'>小计</th>
					<th width='10%' align='center'>操作</th>
				</tr>
			</thead>
			<tbody style="border:1px solid #E5E5E5;">
				<?php
					$total = 0;
					//遍历输出session里面的商品信息
					if(!empty($_SESSION['shoplist'])){
						foreach ($_SESSION['shoplist'] as $shop) {
							echo "<tr style='border:1px solid #E5E5E5;'>";
							echo "<td align='center' style='border:1px solid #E5E5E5;'>{$shop['goods']}</td>";
							echo "<td align='center' style='border:1px solid #E5E5E5;'><a href=\"../details.php?goods={$shop['goods']}\"><img src=\"../../public/uploads/s_{$shop['picname']}\"></a></td>";
							echo "<td align='center' style='border:1px solid #E5E5E5;'>{$shop['price']}</td>";
							echo '<td align="center" style="border:1px solid #E5E5E5;">';
							echo "<a href=\"buycar.php?a=reduce&id={$shop['id']}\" style=\"display:inline-block; border:1px solid ##E5E5E5;padding:3px;font-size:20px;\">-</a>";
							echo '<input type="text" id="shopnum" value="'.$shop['num'].'" size="2">';	  
							echo "<a  href=\"buycar.php?a=add&id={$shop['id']}\" style=\"display:inline-block; border:1px solid ##E5E5E5;padding:3px;font-size:20px;\">+</a></td>";
							echo "<td align='center' style='border:1px solid #E5E5E5;'>".($shop['num']*$shop['price'])."</td>";
							echo "<td align='center' style='border:1px solid #E5E5E5;'><a href='buycar.php?a=del&id={$shop['id']}'>删除</a></td>";
							echo "</tr>";
							$sum += $shop['num'];
							$total += $shop['num']*$shop['price'];
						}
					}else{
						echo "<span style='font-size:16px;'>购物车还没东西哦~~~赶紧去抢购吧";
						echo '<a href="../index.php">回首页</a></span>';
					}
				?>
				<tr>
					<td style="font-size: 16px;">商品总数：<?php echo $sum;?></td>
					<td></td>
					<td></td>
					<td></td>
					<td></td>
					<td colspan=3 class='total' style="font-size: 14px;">
						应付总额： <strong style="font-size: 20px;"><?php echo $total;?></strong> 
					</td>
				</tr>
				<tr>
					<td colspan="6" align="right"><a id="cenCar" href="buycar.php?a=clear">清空购物车</a></td>
				</tr>
			</tbody>
		</table>
		<!-- 收货地址 -->
		<!-- 订单提交 -->
			<div class='bottom'>
				<input type="submit" class='submit' value="提交订单">
			</div>
		</form>
	</div>
</body>
</html>